package com.yuyou.train.lesson.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.lesson.mapper.RuleMapper;
import com.yuyou.train.lesson.service.ILessonResourceRuleRelationService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.lesson.service.IRuleService;
import com.yuyou.train.service.api.lesson.dto.RuleDetailDTO;
import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleRelation;
import com.yuyou.train.service.api.lesson.entity.Rule;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.enums.CommEnums;
import com.yuyou.train.util.message.lesson.LessonMess;
import com.yuyou.train.util.message.lesson.RuleMess;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class RuleServiceImpl implements IRuleService{

	@Autowired
	private RuleMapper ruleMapper;
	
	@Autowired
	private ILessonResourceRuleRelationService lesResRuleRelService;
	
	@Autowired
	private ILessonService lessonService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return ruleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Rule record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return ruleMapper.insert(record);
	}

	@Override
	public Rule selectByPrimaryKey(String id) {
		return ruleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Rule record) {
		return ruleMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ResBoolSimpleInfo<String> delRule(String id) {
		//资源关联课程其他不能删除	
		ResBoolSimpleInfo<String> res=hasLessonRel(id);
		if(!res.isSuccess()){
			return res;
		}
		
		this.deleteByPrimaryKey(id);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResBoolSimpleInfo<String> addOrUpdateRule(Rule record) {	
		String resourceType=record.getResourceType();
		if(resourceType.equals(CommEnums.resourceType.video.getValue())
				||resourceType.equals(CommEnums.resourceType.music.getValue())){
			//视频规则（1：时长）
			if(record.getDuration()==null && record.getDuration()<0){
				return new ResBoolSimpleInfo<String>(false,RuleMess.ERR_ADD_NOT_TIME);
			}
		}
		if(resourceType.equals(CommEnums.resourceType.file.getValue())){
			//文件规则（1：提交）
			record.setNeedAction(CommEnums.isOrNot.yes.getValue());	
		}
		if(resourceType.equals(CommEnums.resourceType.exam.getValue())){	
			//试卷规则（1：提交）
			record.setNeedAction(CommEnums.isOrNot.yes.getValue());	
			if(record.getNeedScore()==null){
				return new ResBoolSimpleInfo<String>(false,RuleMess.ERR_SCORE_EMPTY);
			}		
		}
		if(resourceType.equals(CommEnums.resourceType.hwJustCommit.getValue())||
				resourceType.equals(CommEnums.resourceType.hwNeedComment.getValue())){
			//作业（提交型作业）
			record.setNeedAction(CommEnums.isOrNot.yes.getValue());	
			if(BooleanUtils.toBoolean(record.getNeedComment())|| record.getNeedComment().equals("1")){
				record.setNeedComment(CommEnums.isOrNot.yes.getValue());
			}
			if(record.getNeedScore()!=null){
				record.setNeedComment(CommEnums.isOrNot.yes.getValue());
			}	
		}
		if(resourceType.equals(CommEnums.resourceType.thought.getValue())){
			//感想规则（1：提交）
			record.setNeedAction(CommEnums.isOrNot.yes.getValue());	
		}
				
		if(!StringUtils.isEmpty(record.getId())){
			ResBoolSimpleInfo<String> res=hasLessonRel(record.getId());
			if(!res.isSuccess()){
				return res;
			}
			this.updateByPrimaryKeySelective(record);
		}else{
			this.insert(record);
		}

		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<RuleDetailDTO> findRulePageList(Pagination<RuleDetailDTO> p) {
		return ruleMapper.findRulePageList(p);
	}

	@Override
	public int findRulePageCount(Pagination<RuleDetailDTO> p) {
		return ruleMapper.findRulePageCount(p);
	}

	@Override
	public RuleDetailDTO selectDetailByPrimaryKey(String id) {
		return ruleMapper.selectDetailByPrimaryKey(id);
	}

	@Override
	public ResBoolSimpleInfo<String> hasLessonRel(String id){
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		LessonResourceRuleRelation lessResRuleRel=new LessonResourceRuleRelation();
		lessResRuleRel.setRuleId(id);
		List<LessonResourceRuleRelation> listRel=lesResRuleRelService.findLesResRuleRel(lessResRuleRel);
		if(listRel.size()>0){
			StringBuilder lesName=new StringBuilder();
			listRel.forEach(item -> {
				Lesson l=lessonService.selectByPrimaryKey(item.getLessonId());
				lesName.append(l.getName()+";");
				});
			res.setSuccess(false);
			res.setMessage(String.format(LessonMess.ERR_UPDATE_HAS_LESS_RULE, lesName.toString()));
			return res;
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
//	@Override
//	public ResBoolSimpleInfo<String> isLessonUse(String ruleId,List<LessonResourceRuleRelation> listRel){
//		LessonResourceRuleRelation lessResRuleRel=new LessonResourceRuleRelation();
//		lessResRuleRel.setRuleId(ruleId);
//		if(listRel==null){
//			listRel=lesResRuleRelService.findLesResRuleRel(lessResRuleRel);
//		}
//		for(LessonResourceRuleRelation rel:listRel){
//			String lessonId=rel.getLessonId();
//			ResBoolSimpleInfo<String> res=lessonService.isLessonUse(lessonId);
//			if(!res.isSuccess()){
//				return res;
//			}
//		}
//		return ResBoolSimpleInfo.getSuccessInfo();
//	}
	

}